package com.rabbit.topic;

import java.io.IOException;

import com.rabbit.RabbitFactory;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.ShutdownSignalException;

/**
 * @author CUIJIAJUN
 * @date   2017年2月15日
 * 
 */
//接收kernel.*消息  
public class ReceiveLogsTopicForKernel {
    private static final String EXCHANGE_NAME = "topic_logs";    
	/**
	 * @param args
	 * @throws IOException 
	 * @throws InterruptedException 
	 * @throws ConsumerCancelledException 
	 * @throws ShutdownSignalException 
	 */
	public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
		Connection connection = RabbitFactory.creatConnection();
		Channel channel = connection.createChannel();
		channel.exchangeDeclare(EXCHANGE_NAME, "topic");
		// 随机生成一个队列   
		String queueName = channel.queueDeclare().getQueue();
		System.out.println(queueName);
        //接收所有与kernel相关的消息    
		channel.queueBind(queueName, EXCHANGE_NAME, "kernel.*");

        System.out.println(" [*] Waiting for messages about kernel. To exit press CTRL+C");    
    
        QueueingConsumer consumer = new QueueingConsumer(channel);    
        channel.basicConsume(queueName, true, consumer);    
    
        while (true)    
        {    
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();    
            String message = new String(delivery.getBody());    
            String routingKey = delivery.getEnvelope().getRoutingKey();    
    
            System.out.println(" [x] Received routingKey = " + routingKey    
                    + ",msg = " + message + ".");    
        } 
		
		
		
	}

}
